<template>
	<view class="cu-form-group margin-top">
		<view class="title">输入</view>
		<input
			placeholder="请输入条码"
			:value="value"
			@input="onInputChange"
			name="todo-input"
			@confirm="onInputConfirm"
			:focus="focus"
			@focus="onInputFocus"
			@blur = "onInputBlur"
			@click="onInputClick"
			
		/>
		<button class="cu-btn bg-green shadow" @click="onCameraClick"><text class="lg text-white cuIcon-camerafill"></text></button>
		
	</view>
</template>

<script>
	export default {
		data(){
			return {
				clickedInput:false
			}
		},
		model: {
			prop: 'value',
			event: 'input'
		},
		props: {
			value:{
				type:String,
				default(){
					return ""
				}
			},
			focus:{
				type:Boolean,
				default(){
					return false;
				}
			}
		},
		mounted() {
			 console.log( "home-监听页面显示");
			//执行频率：game（20ms/次）、ui（60ms/次）、normal（200ms/次）
			this.checkTask = setInterval(()=>{
				if(this.focus&&!this.clickedInput){
					uni.hideKeyboard();//隐藏软键盘
				}
				// plus.key.hideSoftKeybord();
			},10);
			
		},
		destroyed() {
			if(this.checkTask){
				clearInterval(this.checkTask)
			}
				
		},
		
		methods:{
			hideKeyboard(){
				this.clickedInput = false
			},
			onInputClick(){
				this.clickedInput=true
				if(this.checkTask){
					clearInterval(this.checkTask)
				}
			},
			onInputChange(e){
				
				this.$emit('input',(e.detail.value + '').toUpperCase())
			},
			onInputBlur(e){
				console.log('onInputBlur',e)
				this.$emit('focusChange',false);
				this.$emit('blur',false)
				// this.isClickShow  = false
				//发生过s失去焦点后，不在执行隐藏键盘
				if(this.checkTask){
					clearInterval(this.checkTask)
				}
			},
			onInputFocus(e){
				if(this.focus){
					return 
				}
				console.log('onInputFocus',this.focus,e)
				this.$emit('focusChange',true)
				 
			},
			onInputConfirm(e){
				this.$emit('confirm',e)
			},
			onCameraClick(){
				// this.$refs.refSearchBar.searchValue("aaaa")
				uni.scanCode({
					success:(ret)=> {
						const arg = {detail:{value:ret.result}}
						// this.$emit('input',(ret.result + '').toUpperCase())
						this.onInputChange(arg)
						this.$nextTick(()=>{
							this.onInputConfirm(arg)
						})
						
						// this.$refs.refSearchBar.searchValue()
						// this.$nextTick(()=>{
						// 	this.onSearchConfirm({
						// 		value:ret.result
						// 	})
						// })
						
						// console.log(ret)
					}
				})
			}
		}
	}
</script>

<style>
</style>
